﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="DataService.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using IveelySE.Common;

#endregion

namespace IveelySE.IDFS.Store
{
    /// <summary>
    ///   数据操作服务
    /// </summary>
    public class DataService
    {
        /// <summary>
        ///   文件块
        /// </summary>
        private static Block _block = new Block();

        /// <summary>
        ///   统计标示
        /// </summary>
        private static int _count;

        public static PObject DealWithRead(byte[] msg)
        {
            var obj = (PObject) Serialize.Byte2Obj(msg);
            //如果对象为空
            if(obj == null)
            {
                //则中断本次对话
                return new PObject("NULL", "为空");
            }
            //获取该文件的块
            var block = (Block) LocalFile.GetObj(obj.FileName);
            //如果该块为空
            return block == null ? new PObject("NULL", "为空") : block.Find(int.Parse(obj.Value.ToString()));
        }

        public static PObject DealWithWrite(byte[] msg)
        {
            var Obj = (PObject) Serialize.Byte2Obj(msg);

            //如果文件名为空
            if(Obj.FileName == "")
            {
                //直接返回
                return new PObject("NULL", "为空");
            }
            //获取该文件的块
            var block = (Block) LocalFile.GetObj(Obj.FileName);
            //如果该块为空
            if(block == null)
            {
                //新建一个块
                block = new Block();
                //设定块的文件名
                block.FileName = Obj.FileName;
            }
            //块中增加对象数据
            block.Add(Obj, msg.Length);
            //将块写回对象文件
            LocalFile.SetObj(Obj.FileName, block);
            return new PObject("OK", "OK");
        }

        public static bool LocalSave(string fileName, object obj)
        {
            bool stored = false;
            var Obj = new PObject(fileName, obj);

            //如果文件名为空
            if(Obj.FileName != "")
            {
                //获取该文件的块
                // Block block = (Block)LocalFile.GetObj(Obj.fileName);
                //如果该块为空
                if(_block == null)
                {
                    //新建一个块
                    _block = new Block();
                    //设定块的文件名
                    _block.FileName = Obj.FileName;
                }
                //块中增加对象数据
                stored = _block.Add(Obj, 1);
                _count++;
                if(_count%10 == 0)
                {
                    LocalFile.SetObj(Obj.FileName, _block);
                }
                //将块写回对象文件
                //stored = stored && LocalFile.SetObj(Obj.fileName, block);
            }
            return stored;
        }

        public static PObject LocalRead(int lineNum, string fileName)
        {
            var block = (Block) LocalFile.GetObj(fileName);
            return block.Find(lineNum);
        }

        public static PObject LocalReadWhenSearch(int lineNum, string fileName)
        {
            var block = (Block) LocalFile.GetObjWhenSearch(fileName);
            return block.FindWhenSearch(lineNum);
        }
    }
}